//
//  TLRewardsTableViewCell.m
//  WXReader
//
//  Created by huaya on 2025/9/6.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLRewardsTableViewCell.h"

@interface TLRewardsTableViewCell ()
@property (weak, nonatomic) IBOutlet UIView *cView;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *cycLab;
@property (weak, nonatomic) IBOutlet UILabel *expiresLab;
@property (weak, nonatomic) IBOutlet UILabel *bonusLab;
@property (weak, nonatomic) IBOutlet UIButton *checkBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomLineView;

@end

@implementation TLRewardsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.checkBtn.layer.masksToBounds = YES;
    self.checkBtn.layer.cornerRadius = 34/2;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setBottomCorners{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.bottomLineView.hidden = YES;
        self.cView.width = SCREEN_WIDTH - 16*2;
        [self.cView addRoundingCornersWithRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight andCornerRadii:8];
        
    });
    
}
- (void)setDefultCorners{

    self.bottomLineView.hidden = NO;
    
    self.cView.width = SCREEN_WIDTH - 16*2;
    
    [self.cView addRoundingCornersWithRoundingCorners:UIRectCornerAllCorners andCornerRadii:0];
    
}
- (void)setTaskModel:(TLTaskModel *)taskModel{
    if (taskModel) {
        _taskModel = taskModel;
        
        self.bottomLineView.hidden = NO;
        
        self.titleLab.text = taskModel.taskName;
        
        self.expiresLab.text = [NSString stringWithFormat:@"Expires on %@",taskModel.taskExpireTime];
        
//        self.expiresLab.text = taskModel.taskDescription;
        
        self.bonusLab.text = [NSString stringWithFormat:@"+%@",taskModel.rewardCoins];
        
        self.cycLab.text = [NSString stringWithFormat:@"%@/%@",taskModel.userReadMinutes,taskModel.requiredMinutes];
        
        if (taskModel.taskStatus.intValue == 0) {
            //0，未完成 1.已完成(未领取) 2.已领取
            [self.checkBtn setBackgroundColor:[UIColor hex_F6F6FE]];
            [self.checkBtn setTitleColor:[UIColor hex_9E6DFC] forState:UIControlStateNormal];
            self.checkBtn.userInteractionEnabled = YES;
            [self.checkBtn setTitle:@"Go" forState:UIControlStateNormal];
        }else if (taskModel.taskStatus.intValue == 1){
            [self.checkBtn setBackgroundColor:[UIColor hex_9E6DFC]];
            [self.checkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.checkBtn.userInteractionEnabled = YES;
            [self.checkBtn setTitle:@"Claim" forState:UIControlStateNormal];
        }else{
            [self.checkBtn setBackgroundColor:[UIColor hex_EEEEEE]];
            [self.checkBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.checkBtn.userInteractionEnabled = NO;
            [self.checkBtn setTitle:@"Claimed" forState:UIControlStateNormal];
        }
        
//        [self.cView addRoundingCornersWithRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight andCornerRadii:0];
    }
}

- (IBAction)btnAction:(id)sender {
    
    if (self.taskModel.taskStatus.intValue == 0) {
        //跳转到首页或者短故事
        if (self.taskModel.worksId && self.taskModel.worksId.intValue != 0) {
            TLShortStoryViewController *vc = [TLShortStoryViewController new];
            vc.worksId = [NSString stringWithFormat:@"%@",self.taskModel.worksId];
            [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
        }else{
            [[TL_ViewHelper getCurrentViewController].navigationController popToRootViewControllerAnimated:YES];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Change_Tabbar_Index object:@"1"];
            });
        }
        
    }else if (self.taskModel.taskStatus.intValue == 1) {
        //获取奖励
        UIView *loadinView = [TL_TopAlertManager showLoading:self];
        MJWeakSelf
        
        [APIManager sendPostRequest:[APIURLManager taskReceiveReward:self.taskModel.taskId.intValue] parameters:@{} model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
            
            [loadinView removeFromSuperview];
            
            if (success) {
                
//                int rCoins = requestModel.data[@"rewardCoins"];
//                int expireDays = requestModel.data[@"expireDays"];
                
                MK_BLOCK_EXEC(weakSelf.checkInBlock,[NSString stringWithFormat:@"%@",requestModel.data[@"rewardCoins"]],[NSString stringWithFormat:@"%@",requestModel.data[@"expireDays"]]);
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
            
            [loadinView removeFromSuperview];
        }];
        
        
    }else{
        //nothing
        
        
    }
    
}
@end
